// pages/detail/detail.js
Page({

  data: {
    activeObj:{},

    // 是否已经报名
    isJoin:false,

    // 查看是否登录
    isLogin:false,
    type:"",
    reasonList:[]
  },

  onLoad: function (options) {
    this.setData({
      type:options.type
    })
    wx.showLoading({
      title: '加载中',
      mask: true,
    });
      
    if(wx.getStorageSync('isLogin')){
      this.setData({
        isLogin:true
      })
    }
    console.log('----',options);
    const that = this;
    wx.cloud.callFunction({
      name:'downDetail',
      data:{
        id:options.id
      },
      success:res=>{
        // console.log('输出详细数据',res);
        that.setData({
          activeObj:res.result.data
        })

        // 将富文本内容展示出来
        that.onEditorReady(res.result.data.content);

        wx.hideLoading();
      }


    });


    // 判断本人是否已经报名参加了
    wx.cloud.callFunction({
      name:'isJoin',
      data:{
        id:options.id
      },
      success:res=>{
        // console.log('判断是否报名成功',res);

        if(res.result&&res.result.data.length!==0){
          that.setData({
            isJoin:true
          })
        }
      }
    })


    // 当为举报类型的才显示
    if(options.type === "report"){
      // 加载数据
      wx.cloud.callFunction({
        name:"downReportReasons",
        data:{
          type:0,
          activeId:options.id,
          openid:""
        },
        success:res=>{
          console.log("下载举报理由成功",res.result.data[0]);
          that.setData({
            reasonList:res.result.data[0].reasonList
          })
        }
      })
    }
  },



  // 活动介绍（富文本编辑）
  onEditorReady(content){
    const that = this;
    wx.createSelectorQuery().select('#editor')
    .context(function(res){
      // console.log('编辑器初始化完成触发',res)
      that.editorCtx = res.context
      // 如果存储中有编辑内容 则展示出来
      that.editorCtx.setContents({
        html:content,
        success:re=>{
          console.log('展示成功',re)
        }
      })
    }).exec()
  },



  // 跳转到活动发布者主页
  navUserHome(e){
    console.log("跳转到活动发布者详情页面",e.currentTarget)
    const openid = e.currentTarget.dataset.openid;
    wx.navigateTo({
      url:`/pages/userCenter/userCenter?openid=${openid}`
    })
  },

  // 分享
  onShareAppMessage(e){
    // console.log(e)
    const {surfacePlot,title,_id} = this.data.activeObj;
    return{
      title:title,
      path:`/pages/detail/detail?id=${_id}`,
      imageUrl:surfacePlot
    }
  },


  // 报名跳转
  signUp(e){
    // console.log(e);
    if(!wx.getStorageSync('isLogin')){
      // 如果没有登录 先进行登录
      wx.showModal({
        title: '提示',
        content: '请先进行登录授权',
        showCancel: true,
        cancelText: '不了',
        cancelColor: '#000000',
        confirmText: '好的',
        confirmColor: '#3CC51F',
        success: (result) => {
          if (result.confirm) {   
            wx.switchTab({
              url: '/pages/mine/mine',
            });
          }
        },
      });

      return;
    }
    const id = e.currentTarget.id;
    wx.navigateTo({
      url: `/pages/signUp/signUp?id=${id}`
    });
  },


  // 没有登录先进行登录
  goToLogin(){
    wx.switchTab({
      url: '/pages/mine/mine',
    });
  },

  // 跳转到举报中心
  toReport(){
    wx.navigateTo({
      url:`/pages/report/report?type=0&&id=${this.data.activeObj._id}`
    })
  },

  // 加入黑名单中，活动该状态进行改变
  darkList(){
    let {activeObj} = this.data;
    wx.cloud.callFunction({
      name:"upActiveStatus",
      data:{
        id:activeObj._id
      },
      success:res=>{
        console.log("加入黑名单",res);
      }
    })

    wx.cloud.callFunction({
      name:"upDateReasonType",
      data:{
        id:activeObj._id,
        openid:""
      },
      success:res=>{
        console.log("举报里面不在加载",res);
        wx.navigateTo({
          url: '/pages/admin/admin',
        });
      },
      fail:e=>{
        console.log("失败",e)
      }
    })
  }
})